﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class ConnOperation
	{
		public UIDocument UIDoc { get; set; }

		public ConnDuctFrom ConnForm { get; set; }

		public List<Duct> SelectedDucts { get; set; }

		public List<XYZ> SelectedPoints { get; set; }

		public string ErrorString { get; set; }

		public ConnOperation(UIDocument uiDoc)
		{
			this.UIDoc = uiDoc;
			this.SelectDuctsMethod = new SelectConnDucts(uiDoc);
			this.SelectedDucts = new List<Duct>();
			this.SelectedPoints = new List<XYZ>();
		}

		public virtual bool Execute()
		{
			bool flag = false;
			this.fittingStyle = this.ConnForm.connStyle;
			if (this.SelectDucts())
			{
				this.GetRuleList();
				flag = this.Connect();
			}
			if (flag)
			{
				this.SetRuleList();
			}
			return flag;
		}

		public virtual void GetRuleList()
		{
		}

		public virtual void SetRuleList()
		{
		}

		public virtual bool SelectDucts()
		{
			return false;
		}

		public virtual bool Connect()
		{
			return false;
		}

		public bool DuctSizeEqual(IList<Duct> ducts)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			return Math.Abs(hypiping.GetDiameter() - hypiping2.GetDiameter()) < 1E-06;
		}

		public bool CheckDuctShape(IList<Duct> ducts)
		{
			if (ducts.Count == 2)
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				return hypiping.Shape == hypiping2.Shape;
			}
			if (ducts.Count == 3)
			{
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(ducts[2]);
				return hypiping3.Shape == hypiping4.Shape && hypiping4.Shape == hypiping5.Shape;
			}
			if (ducts.Count == 4)
			{
				YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(ducts[2]);
				YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(ducts[3]);
				return hypiping6.Shape == hypiping7.Shape && hypiping7.Shape == hypiping8.Shape && hypiping8.Shape == hypiping9.Shape;
			}
			return false;
		}

		public ConnectDuctStyle fittingStyle = ConnectDuctStyle.Empty;

		public SelectConnDucts SelectDuctsMethod;

		public IList<RoutingPreferenceRule> RuleList;
	}
}
